package com.shqcjd.sinopecdemomobile.ui.activity.tipwizard;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;

import com.shqcjd.sinopecdemomobile.R;
import com.shqcjd.sinopecdemomobile.api.ApiService;
import com.shqcjd.sinopecdemomobile.helper.rxjavahelper.RxObserver;
import com.shqcjd.sinopecdemomobile.helper.rxjavahelper.RxSchedulersHelper;
import com.shqcjd.sinopecdemomobile.model.ResponseData;
import com.shqcjd.sinopecdemomobile.ui.adapter.TipWizardImageAdapter;
import com.shqcjd.sinopecdemomobile.ui.base.BaseActivity;
import com.shqcjd.sinopecdemomobile.ui.base.BasePresenter;
import com.shqcjd.sinopecdemomobile.util.SpacesItemDecoration;
import com.shqcjd.sinopecdemomobile.util.UIUtils;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

/**
 * 操作向导视频
 */
public class TipWizardImageActivity extends BaseActivity {

    @BindView(R.id.rv_img_list)
    RecyclerView rvImgList;
    @BindView(R.id.ib_finish)
    ImageButton ibFinish;
    private Context mContext;
    private int stepDetailId;
    private View notDataView;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_tip_wizard_image;
    }

    @Override
    public void initView() {
        mContext = this;
        stepDetailId = getIntent().getIntExtra("stepDetailId",0);
        if (stepDetailId==0){
            finish();
            return;
        }
        fetchData();

        rvImgList.setLayoutManager(new GridLayoutManager(mContext,2));
        int leftRight = UIUtils.dip2px(22);
        int topBottom = UIUtils.dip2px(22);
        rvImgList.addItemDecoration(new SpacesItemDecoration(leftRight, topBottom));
        notDataView = getLayoutInflater().inflate(R.layout.layout_no_data, (ViewGroup) rvImgList.getParent(), false);
    }

    private void fetchData(){
        ApiService.getImageList(stepDetailId)
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData<ArrayList<String>>>() {
                    @Override
                    public void _onSubscribe(Disposable d) {
                        super._onSubscribe(d);
                    }

                    @Override
                    public void _onNext(ResponseData<ArrayList<String>> retData) {
                        if (retData.retCode == 10000) {
                            TipWizardImageAdapter wizardImageAdapter = new TipWizardImageAdapter();
                            if (retData.data==null||retData.data.isEmpty()){
                                wizardImageAdapter.setEmptyView(notDataView);
                            }else {
                                wizardImageAdapter.setNewData(retData.data);
                            }
                            rvImgList.setAdapter(wizardImageAdapter);
                        }
                    }

                    @Override
                    public void _onError(String errorMessage) {
                        Log.e("tou", errorMessage);
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                    }
                });
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState){
        super.onSaveInstanceState(savedInstanceState);
        savedInstanceState.putInt("stepDetailId", stepDetailId);
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState){
        super.onRestoreInstanceState(savedInstanceState);
        stepDetailId = savedInstanceState.getInt("stepDetailId");
    }

    @OnClick(R.id.ib_finish)
    public void onViewClicked() {
        finish();
    }
}
